package com.yl.plugin.method;

import com.yl.plugin.method.base.BaseMethod;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;

/**
 * @author liuxubo
 */
public class ListSelectiveMethod implements BaseMethod {
    @Override
    public int order() {
        return 1;
    }

    @Override
    public Method buildMethod(IntrospectedTable introspectedTable) {
        // 实体类类型
        FullyQualifiedJavaType modelType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());

        // 访问类型：public
        JavaVisibility methodVisibility = JavaVisibility.PUBLIC;

        // 返回类型：List
        FullyQualifiedJavaType returnType = new FullyQualifiedJavaType("List");
        // 设置泛型参数
        returnType.addTypeArgument(modelType);

        // 设置参数类型是对象
        FullyQualifiedJavaType parameterType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
        Parameter methodParameter = new Parameter(parameterType, StringUtils.uncapitalize(parameterType.getShortName()));

        // 创建方法对象
        Method method = new Method("list");
        // 设置访问类型
        method.setVisibility(methodVisibility);
        // 设置返回类型对象
        method.setReturnType(returnType);
        // 设置方法参数
        method.addParameter(methodParameter);

        // 设置备注
        method.addJavaDocLine("/**");
        method.addJavaDocLine(" * 列表查询");
        method.addJavaDocLine(" * ");
        method.addJavaDocLine(" * @param " + methodParameter.getName());
        method.addJavaDocLine(" */");

        return method;
    }

}
